#ifndef ECGDETECTION_H
#define ECGDETECTION_H
#include <QDebug>

#include <QObject>
/* Butterworth filter */
struct Filter
{
    float b0, b1, b2, a1, a2;
    float x0, x1, x2, y1, y2;
};
struct QRS_message {
    int16_t rpos[20];
    int16_t rpos2[20];

    int16_t rpos_count1;
    int16_t rpos_count;

} ;
class EcgDetection : public QObject {
    Q_OBJECT
public:
    explicit EcgDetection(QObject* parent = nullptr);
    void recognition(QList<int>& ecgSignalData);
private:
    enum FilterType {
        FT_HIGH_PASS,
        FT_LOW_PASS,
        FT_REJECTION
    };
    enum MARK_QRS {
        MARK_NO_QRS,
        MARK_QRS
    };
    QList<unsigned char> result;//结果
    void normalization(const QList<int>& ecgSignalData, QList<float>& norSignalData);
    int QRSCalibration(const QList<float>& norSignalData,QRS_message* qrs_message);
    int DetectQrsPeaks(const QList<float>& norSignalData,QList<unsigned char>& result,float rate);
    int FilterSignal(const QList<float>& norSignalData,QList<float>& filSignalData, float rate);
    Filter InitFilter(float cutoff_hz, float rate, FilterType type);
    void ResetFilter(Filter* filter);
    void fill(Filter* filter, float cutoff, int is_hpf);
    void FilterData(Filter* filter, QList<float> &data);
    float percentiles(const QList<float>& data, int percent);
    void Normalize(QList<float>& values);
    void ComputeDerivative(const QList<float>& filSignalData, QList<float>& derivative);
    void ArrayPow2(QList<float>& filSignalData);
    void WindowIntegration(const QList<float>& signal, QList<float>& output, int window_size);
    int Thresholding2(const QList<float>& integrated, int min_rr_width, int max_rr_width, QList<unsigned char>& result);
    void SubtractDelay(QList<unsigned char>& qrs_detection_result, int samples_delay);

signals:
};

#endif // ECGDETECTION_H
